﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.CommBodyEditor;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased
{

	/// <summary>
	/// Declaration of FieldPickerCtrl class
	/// Control for displaying a list of fields that can be
	/// inserted into a comm-body
	/// </summary>
	public partial class FieldPickerCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		CommBodyEditCtrl m_parentCtrl = null;

		/////////////////////////////////////////////////////////////
		// Construction

		public FieldPickerCtrl()
		{

			// Load xaml resources
			InitializeComponent();

			// Load sections
			List<string> strSections = StufferDefinitions.GetSections();
			for( int i1=0; i1<strSections.Count; i1++ )
				cboSection.Items.Add(strSections[i1]);
			cboSection.SelectedIndex = 0;

			// Setup controls
			_setupControls();

		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{

			// Retain the sibling window
			m_parentCtrl = (sender as FrameworkElement).FindParent(typeof(CommBodyEditCtrl)) as CommBodyEditCtrl;

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void cboSection_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Load the fields
			string strSection = cboSection.SelectedItem.ToString();
			List<string> strFields = StufferDefinitions.GetFields(strSection);
			lstFields.Items.Clear();
			for (int i1 = 0; i1 < strFields.Count; i1++)
				lstFields.Items.Add(strFields[i1]);

		}

		private void lstFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		private void butInsert_Click(object sender, RoutedEventArgs e)
		{

			// Inform parent control
			m_parentCtrl.InsertField( lstFields.SelectedItem.ToString() );

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _setupControls()
		{

			// Enable/disable controls
			butInsert.IsEnabled = (lstFields.SelectedIndex != -1);

		}

	}

}
